package com.ph.member.api.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ph.base.BaseController;
import com.ph.member.api.controller.qo.MemberIntegralQo;
import com.ph.member.api.service.MemberIntegralDataService;
import com.ph.shopping.common.core.constant.PageConstant;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.container.ContainerUtil;
import com.ph.shopping.common.util.date.ThreadLocalDateUtil;
import com.ph.shopping.common.util.excel.ExcelBean;
import com.ph.shopping.common.util.excel.ExcelUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.transform.MoneyTransUtil;
import com.ph.shopping.facade.member.dto.MemberIntegralDto;
import com.ph.shopping.facade.member.service.IMemberIntegralService;
import com.ph.shopping.facade.member.vo.MemberScoreVo;
import com.ph.shopping.facade.score.service.IScoreService;
import com.ph.shopping.facade.score.vo.MemberScoreVo2;
/**
 * 
* @ClassName: MemberIntegralController  
* @Description: 会员积分
* @author tony  
* @date 2017年3月23日  
*
 */
@Controller
@RequestMapping("api/memberintegral")
public class MemberIntegralController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(MemberIntegralController.class);
	
	@Reference(version = "1.0.0", retries = 1, timeout = 10000)
	private IMemberIntegralService memberIntegralService;
	
	@Reference(version = "1.0.0", retries = 1, timeout = 10000)
	private IScoreService scoreService;
	
	@Autowired
	private MemberIntegralDataService memberIntegralDataService;
	/**
	 * 
	* @Title: list  
	* @Description: 加载会员积分列表
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value = "/memberintegralList", method = RequestMethod.GET)
	public String list(HttpServletRequest request,Model model) {
		try {
			model.addAttribute("memberId", request.getParameter("memberId"));
			Result result = memberIntegralService.getMemberIntegralSource();
			if(result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())){
				model.addAttribute("data", result.getData());
			}
		} catch (Exception e) {
			log.error("加载会员积分来源字典数据错误", e);
		}
		return "/member/memberIntrgralList";
	}
	/**
	 * 
	* @Title: listPage  
	* @Description: 加载会员积分数据列表 
	* @param @param request
	* @param @param qo
	* @param @return    参数  
	* @return Result    返回类型  
	* @throws
	 */
	@RequestMapping("/listPage")
	@ResponseBody
	public Result listPage(HttpServletRequest request, MemberIntegralQo qo) {
		log.info("加载会员积分列表参数 MemberIntegralQo = {} ", JSON.toJSONString(qo));
		Result result = getResult(ResultEnum.MEMBER_NO_DATA);
		try {
			MemberIntegralDto dto = new MemberIntegralDto();
			BeanUtils.copyProperties(qo, dto);
			dto.setCreateStartTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateStartTime()));
			dto.setCreateEndTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateEndTime()));
			if (dto.getPageNum() == null) {
				dto.setPageNum(PageConstant.pageNum);
			}
			if (dto.getPageSize() == null) {
				dto.setPageSize(PageConstant.pageSize);
			}
			result = memberIntegralService.getMemberIntegralInfoLsitByPage(dto);
		} catch (Exception e) {
			log.error("加载会员积分列表错误", e);
		}
		return result;
	}
	/**
	 * 
	* @Title: getScoreByMemberId  
	* @Description:根据会员ID 获取积分数据
	* @param @param request
	* @param @param memberId
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/getScoreByMemberId")
	@ResponseBody
	public Map<String, Object> getScoreByMemberId(HttpServletRequest request, Long memberId) {
		log.info("加载会员积分总收支信息 参数 >> ：" + memberId);
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			if(memberId != null){
				MemberScoreVo ms = updateMemberScoreVo(scoreService.getMemberScore(memberId));
				log.info("根据会员ID 查询积分返回数据 MemberScoreVo={}",ms);
				if(ms != null){
					map.put("score", ms);
					map.put("code", ResultEnum.SUCCESS.getCode());
				}
			}
		} catch (Exception e) {
			log.error("加载会员积分总收支信息列表错误", e);
		}
		return map;
	}
	/**
	 * 
	 * @Title: updateMemberScoreVo   
	 * @Description: 处理返回的 积分值 统一除以 1000   
	 * @param: @param vo
	 * @param: @return      
	 * @return: MemberScoreVo2      
	 * @throws
	 */
	private MemberScoreVo updateMemberScoreVo(MemberScoreVo2 vo){
		MemberScoreVo result = null;
		if(vo != null){
			result = new MemberScoreVo();
			long drawcashScore = vo.getDrawcashScore();
			result.setDrawcashScore(MoneyTransUtil.transDivisDouble(drawcashScore));
			long enablescore = vo.getEnablescore();
			result.setEnablescore(MoneyTransUtil.transDivisDouble(enablescore));
			long standbyscore = vo.getStandbyscore();
			result.setStandbyscore(MoneyTransUtil.transDivisDouble(standbyscore));
		}
		return result;
	} 
	/**
	 * 
	* @Title: exportExcel  
	* @Description: 会员积分数据导出
	* @param @param request
	* @param @param response
	* @param @param qo    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("/memberExportExcel")
	public void exportExcel(HttpServletRequest request, HttpServletResponse response, MemberIntegralQo qo) {
		log.info("导出会员积分列表参数 MemberPageDto = {} ", JSON.toJSONString(qo));
		try {
			MemberIntegralDto dto = new MemberIntegralDto();
			BeanUtils.copyProperties(qo, dto);
			dto.setCreateStartTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateStartTime()));
			dto.setCreateEndTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateEndTime()));
			Result result = memberIntegralService.getMemberIntegralInfoList(dto);
			log.info("导出会员积分列表返回数据 Result = {} ", JSON.toJSONString(result));
			if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
				Object obj = result.getData();
				if (obj instanceof List) {
					@SuppressWarnings("unchecked")
					List<MemberIntegralDto> list = (List<MemberIntegralDto>) obj;
					ExcelBean exelBean = memberIntegralDataService.getMemberIntegralExcelBean(list);
					ExcelUtil.download(request, response, exelBean);
				}
			}
		} catch (Exception e) {
			log.error("导出会员积分数据列表错误", e);
		}
	}
}
